package com.mark.tool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.mark.io.FileCommon;

public class AnaylysisExctraVisitor {
	
	public AnaylysisExctraVisitor(String filePath,String regexString,String outputResultFloder){
		try {
			List<String> tempLog = FileCommon.singleBrReader(filePath);
			extracIpAndDate(tempLog,regexString,outputResultFloder+"temp.log");
			dailyAnaylysisVisitor(outputResultFloder+"temp.log",outputResultFloder+"result.rs");
			System.out.println(outputResultFloder+"temp.log");
//			boolean   success   =   (new File( outputResultFloder+"temp.log")).delete();
			boolean   success   =    FileCommon.deleteFile(outputResultFloder+"temp.log");
			if(!success){
				System.out.println("The Temp cannot delete!!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void extracIpAndDate(List<String> log ,String regexString,String tempLogFile){
		try{
		for (Iterator<String> iterator = log.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			String[] ar =  string.split(" ");
			if( ar.length > 10){
				if( ar[6].indexOf(regexString) != -1){
					FileCommon.appendFile(tempLogFile, ar[0]+"|"+ar[3].substring(1, 12)+"|"+ar[6]+"\n");
				}
			}
		}
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void dailyAnaylysisVisitor(String dataFile,String outputFile){
		try {
			List<String> tempLog = FileCommon.singleBrReader(dataFile);
			Map<String, Map<String, Integer>> mapDate = new HashMap<String, Map<String, Integer>>();
			//Map<String, Integer> map = new HashMap<String, Integer>();
			for (Iterator<String> iterator = tempLog.iterator(); iterator.hasNext();) {
				String string = (String) iterator.next();
				String[] ar = string.split("\\|");
				String ip = ar[0];
				//System.out.println("ar[1] "+ar[1]);
				String time = ar[1];
				//System.out.println( mapDate.containsKey(time) );
				if( mapDate.containsKey(time)){
					if( mapDate.get(time).containsKey(ip) ){
						mapDate.get(time).put(ip, mapDate.get(time).get(ip)+1);
						mapDate.put(time, mapDate.get(time));
					}else{
						mapDate.get(time).put(ip, 1);
						mapDate.put(time, mapDate.get(time));
						//System.out.println("Test1");
					}
					//tempMap.clear();
				}else{
					Map<String, Integer> ipMap = new HashMap<String,Integer>();
					ipMap.put(ip, 1);
					mapDate.put(time, ipMap);
					//System.out.println("Test2");
				}
			}
			//System.out.println(mapDate.size());
			Iterator<Entry<String, Map<String, Integer>>> it = mapDate.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Map<String, Integer>> object = 
					(Map.Entry<String, Map<String, Integer>>) it.next();
				Map<String, Integer> mapView = (Map<String, Integer>) object.getValue();
				System.out.println( object.getKey() + ":" + mapView.size() );
//				Iterator itIp = mapView.entrySet().iterator();
//				System.out.println("++++++++++++++++++++++++");
//				while (itIp.hasNext()) {
//					Map.Entry object2 = (Map.Entry) itIp.next();
//					System.out.println( object2.getKey() + ":" + object2.getValue() );
//				}
//				System.out.println("++++++++++++++++++++++++");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
	}

}
